package com.icart.controller.personController;

import com.icart.context.HttpUserContext;
import com.icart.context.provider.UserContextProvider;
import com.icart.context.storage.ContextStorage;
import com.icart.context.storage.SessionContextStorageImpl;
import com.icart.repositories.CustinfoRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;


import javax.servlet.http.HttpServletRequest;
import java.util.List;


@Controller
@RequestMapping("/person")
public class PersonmessageController {

    @Autowired
    CustinfoRepository custinfoRepository;
    @Autowired
    HttpServletRequest request;

    /*个人信息首页*/
    @RequestMapping("/personmessage")
    public String personmessage(Model model) {
        ContextStorage storage=new SessionContextStorageImpl(request);
        HttpUserContext userContext=storage.getUserContext("loginuser");

        String sql="select o.custname,o.fm,o.mobile,(select m.dictname from sa_db_dictitem m where m.groupid='CU_CUSTLEVEL' and  m.dictid=o.custlevel)custlevel," +
                "o.custbirthday,o.addr,o.email,o.wxid,o.income,o.idnum,o.points from cu_custinfo o where o.custid="+userContext.getCustid();
        List list=custinfoRepository.createNativeQuery(sql);
        model.addAttribute("custinfo",list.get(0));
        return "person/personmessage/personmessage";
    }

    /*输入真实姓名ajax存储*/
    @RequestMapping("/savecustname")
    @ResponseBody
    public String savecustname(@RequestParam(name = "custname",required = false) String custname, Model model) {
        ContextStorage storage=new SessionContextStorageImpl(request);
        HttpUserContext userContext=storage.getUserContext("loginuser");

        HttpUserContext context= UserContextProvider.create(userContext.getCustid(),custname);
        storage.clear("loginuser");
        storage.save("loginuser",context);
        Integer flag=custinfoRepository.savecustname(custname,userContext.getCustid());
        if(flag>0){
            return "success";
        }else {
            return "fail";
        }
    }

    /*输入手机号名ajax存储*/
    @RequestMapping("/savemobile")
    @ResponseBody
    public String savemobile(@RequestParam(name = "mobile",required = false) String mobile, Model model) {
        ContextStorage storage=new SessionContextStorageImpl(request);
        HttpUserContext userContext=storage.getUserContext("loginuser");

        Integer flag=custinfoRepository.savemobile(mobile,userContext.getCustid());
        if(flag>0){
            return "success";
        }else {
            return "fail";
        }
    }

    /*输入性别ajax存储*/
    @RequestMapping("/savesex")
    @ResponseBody
    public String savesex(@RequestParam(name = "sex",required = false) String sex, Model model) {
        ContextStorage storage=new SessionContextStorageImpl(request);
        HttpUserContext userContext=storage.getUserContext("loginuser");

        Integer flag=custinfoRepository.savesex(sex,userContext.getCustid());
        if(flag>0){
            return "success";
        }else {
            return "fail";
        }
    }

    /*输入客户生日ajax存储*/
    @RequestMapping("/savecustbirthday")
    @ResponseBody
    public String savecustbirthday(@RequestParam(name = "custbirthday",required = false) String custbirthday, Model model) {
        ContextStorage storage=new SessionContextStorageImpl(request);
        HttpUserContext userContext=storage.getUserContext("loginuser");

        Integer flag=custinfoRepository.savecustbirthday(custbirthday,userContext.getCustid());
        if(flag>0){
            return "success";
        }else {
            return "fail";
        }
    }

    /*输入Email ajax存储*/
    @RequestMapping("/saveemail")
    @ResponseBody
    public String saveemail(@RequestParam(name = "email",required = false) String email, Model model) {
        ContextStorage storage=new SessionContextStorageImpl(request);
        HttpUserContext userContext=storage.getUserContext("loginuser");

        Integer flag=custinfoRepository.saveemail(email,userContext.getCustid());
        if(flag>0){
            return "success";
        }else {
            return "fail";
        }
    }

    /*输入月收入 ajax存储*/
    @RequestMapping("/saveincome")
    @ResponseBody
    public String saveincome(@RequestParam(name = "income",required = false) String income, Model model) {
        ContextStorage storage=new SessionContextStorageImpl(request);
        HttpUserContext userContext=storage.getUserContext("loginuser");

        Integer flag=custinfoRepository.saveincome(income,userContext.getCustid());
        if(flag>0){
            return "success";
        }else {
            return "fail";
        }
    }

    /*输入身份证号ajax存储*/
    @RequestMapping("/saveidnum")
    @ResponseBody
    public String saveidnum(@RequestParam(name = "idnum",required = false) String idnum, Model model) {
        ContextStorage storage=new SessionContextStorageImpl(request);
        HttpUserContext userContext=storage.getUserContext("loginuser");

        Integer flag=custinfoRepository.saveidnum(idnum,userContext.getCustid());
        if(flag>0){
            return "success";
        }else {
            return "fail";
        }
    }

    /*输入地址ajax存储*/
    @RequestMapping("/saveaddr")
    @ResponseBody
    public String saveaddr(@RequestParam(name = "regionid",required = false) String regionid, Model model) {
        ContextStorage storage=new SessionContextStorageImpl(request);
        HttpUserContext userContext=storage.getUserContext("loginuser");

        Integer flag=custinfoRepository.saveaddr(regionid,userContext.getCustid());
        if(flag>0){
            return "success";
        }else {
            return "fail";
        }
    }

    /*点击绑定或解绑微信号*/
    @RequestMapping("/saveOrdelwxid")
    @ResponseBody
    public String saveOrdelwxid(@RequestParam(name = "wxid",required = false) String wxid, Model model) {
        ContextStorage storage=new SessionContextStorageImpl(request);
        HttpUserContext userContext=storage.getUserContext("loginuser");

        Integer flag=null;
        if(wxid.equals("绑定")){
            String openid=request.getSession().getAttribute("openid").toString();
            flag=custinfoRepository.savewxid(openid,userContext.getCustid());
        }else {
            flag=custinfoRepository.delwxid(userContext.getCustid());
        }
        if(flag>0){
            return "success";
        }else {
            return "fail";
        }
    }
}